var layer, form, upload, laytpl, util, flow, laydate;
var uploads_url = '/index/uploads/uploadImage.html';
layui.use(['form', 'layer', 'upload', 'laytpl', 'util', 'flow', 'laydate'], function () {
    layer = layui.layer;
    form = layui.form;
    upload = layui.upload;
    laytpl = layui.laytpl;
    util = layui.util;
    flow = layui.flow;
    laydate = layui.laydate;
    //时间选择器
    laydate.render({
        elem: '#test4'
        , type: 'time'
    });

    flow.lazyimg();

    upload.render({
        elem: '#uploads_image',
        url: uploads_url,
        before: function (obj) {
            layer.load(2);
            obj.preview(function (index, file, result) {
                // $(".flex-center").show();
                $('#image_show').attr('src', result);
            });
        },
        done: function (res) {
            layer.closeAll();
            if (res.code == 200) {
                layer.msg('上传成功');
                $("#images_value").val(res.data.url);
            } else {
                return layer.msg('上传失败:' + res.msg);
            }

        },
        error: function () {
            return layer.msg('上传失败');
        }
    });

    upload.render({
        elem: '#photo',
        url: uploads_url,
        before: function (obj) {
            layer.load(2);
            obj.preview(function (index, file, result) {
                $('#photo').attr('src', result);
            });
        },
        done: function (res) {
            layer.closeAll();
            if (res.code == 200) {
                _ajax('/index/user/edit.html', {photo: res.data.url});
            } else {
                return layer.msg('上传失败:' + res.msg);
            }
        },
        error: function () {
            return layer.msg('上传失败');
        }
    });


    form.on('select(mode)', function (data) {

        switch (data.value) {
            case "微信":
                $(".wechat").show();
                $(".alipay").hide();
                $(".bank").hide();
                break;
            case "支付宝":
                $(".alipay").show();
                $(".wechat").hide();
                $(".bank").hide();
                break;
            case "银行卡":
                $(".bank").show();
                $(".alipay").hide();
                $(".wechat").hide();
                break;
            default:
                $(".bank").hide();
                $(".alipay").hide();
                $(".wechat").hide();
        }
    });

    var os = true;
    form.on('submit(save)', function (data) {
        if (os !== true) return false;
        os = false;
        layer.load();
        var btn = $(this);
        btn.attr('disabled', true);
        formObj = $(btn.attr('formObj'));
        url = formObj.attr('action');
        var type = btn.attr('data-type')
        $.ajax({
            type: 'POST',
            url: url,
            data: formObj.serialize(),
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                if (type === 'order') {
                    if (data.code < 1) {
                        os = true;
                        layer.msg(data.msg);
                        btn.removeAttr('disabled');
                        return false;
                    }
                    payment(data);
                    return false;
                }
                switch (data.code) {
                    case 1:
                        layer.msg(data.msg, {time: 1500}, function () {
                            if (data.url) {
                                location.href = data.url;
                            } else {
                                window.location.reload();
                            }
                        });
                        break;
                    case 3:
                        layer.msg(data.msg, {time: 1500}, function () {
                            location.href = document.referrer;
                        });
                        break;
                    case 4:
                        location.href = data.url;
                        break;
                    default:
                        os = true;
                        layer.msg(data.msg);
                        btn.removeAttr('disabled');
                }

            },
            error(e) {
                layer.closeAll();
                btn.removeAttr('disabled');
            }
        });
        return false;
    })

    form.on('select(prov)', function (data) {
        var id = data.value;
        get_area(2, id, '#city');
        $("#county").html('<option value="">请选择</option>');
    });
    form.on('select(city)', function (data) {
        var id = data.value;
        get_area(3, id, '#county');
    });
});

var common = {
    request: function (url, data, type) {
        common.load();
        var btn = $(this);
        btn.attr('disabled', true);
        $.ajax({
            type: type ? type : 'POST',
            url: url,
            data: data,
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                btn.removeAttr('disabled');
                if (data.code == 1) {
                    if (data.url) {
                        location.href = data.url;
                    } else {
                        layer.msg(data.msg, {time: 1000}, function () {
                            window.location.reload();
                        });
                    }

                } else {
                    layer.msg(data.msg);
                }

            },
            error(e) {
                layer.closeAll();
                btn.removeAttr('disabled');
            }
        });
        return false;
    },
    load: function (text = '加载中....') {
        layer.msg('<div><i class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i> ' + text + '', {
            shade: 0.4,
            time: 0
        });
    }
}

function get_area(depth, parent_id, name) {
    $.ajax({
        type: 'GET',
        url: '/index/address/getAreas',
        data: {depth: depth, parent_id: parent_id},
        dataType: "json",
        success: function (data) {
            let is;
            if (depth == 2) {
                is = $("#city_id").val();
            } else {
                is = $("#county_id").val();
            }
            if (data.code == 1) {
                if (data.result.length > 0) {
                    data.is = is;
                    let tpl = document.getElementById('template').innerHTML;
                    laytpl(tpl).render(data, function (html) {
                        $(name).html(html);
                    });
                }
            } else {
                layer.msg('加载失败');
            }
            form.render();
        }
    });
}

function layer_open(title, content) {
    $('.textAlert').show();
    layer.open({
        type: 1,
        shade: [0.8, '#021D33'],
        shadeClose: false,
        closeBtn: 0,
        area: ['300px', 'auto'], //宽高
        title: title,
        content: '<div class="textAlert"><div class="textAlert-count">' + content + '</div><a class="closeTextAlert" href="javascript:close_open();">确定</a></div>',
    });
}

function close_open(type) {
    $('.textAlert').hide();
    layer.closeAll();
    if (type) window.location.reload();
}

var os = true;

function _ajax(url, data, type) {
    if (os !== true) return false;
    os = false;
    layer.load(2);
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        dataType: "json",
        success: function (data) {
            layer.closeAll();
            switch (data.code) {
                case 1:
                    layer.msg(data.msg, {time: 1500}, function () {
                        if (data.url) {
                            location.href = data.url;
                        } else {
                            window.location.reload();
                        }
                    });
                    break;
                case 3:
                    layer.msg(data.msg, {time: 1500}, function () {
                        location.href = document.referrer;
                    });
                    break;
                case 4:
                    location.href = data.url;
                    break;
                case 5:
                    if (window.name != '') {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.window.location.reload();
                    } else {
                        window.location.reload();
                    }
                    break;
                default:
                    os = true;
                    layer.msg(data.msg);
            }
        }
    });
}

$(".load_more").click(function () {
    if ($('.load_more').text() !== '没有更多了') {
        layer.load(2);
        let page = $(this).attr('data-page');
        let type = $(this).attr('data-type');
        let url = $(this).attr('data-url');
        get_list(parseInt(page) + 1, type, url)
        layer.closeAll();
    }
});

function getFlow(type, url, tpl = 'template', end = '没有更多了') {
    var tpls = tpl;
    flow.load({
        elem: '.tpl_list'
        , isLazyimg: true
        , end: '<div class="layui-flow-more" >' + end + '</div>'
        , done: function (page, next) {
            $.ajax({
                type: 'GET',
                url: url,
                data: {page: page, type: type},
                dataType: "json",
                success: function (data) {

                    if (data.code == 1) {
                        if (data.data.length > 0) {
                            var tpl = document.getElementById(tpls).innerHTML;
                            laytpl(tpl).render({data: data.data, page: data.page, type: type}, function (html) {
                                next(html, page < data.pages);
                            });
                        } else {
                            next('', page < data.pages);
                        }
                    }
                }
            });
        }
    });
}

function count_down() {
    $(".count_down").each(function () {
        var that = $(this);
        var count_down = that.attr('data-end');
        var start = new Date("{:date('Y')}", "{:date('m')}", "{:date('d')}", "{:date('H')}", "{:date('i')}", "{:date('s')}");
        count_down = count_down.split(",");
        var end = new Date(count_down[0], count_down[1], count_down[2], count_down[3], count_down[4], count_down[5]);
        util.countdown(end, start, function (date, serverTime, timer) {
            if (date[0] === 0 && date[1] === 0 && date[2] === 0 && date[3] === 0) {
                that.text('加载中');
            } else {
                var str = date[1] + ' 时 ' + date[2] + ' 分 ' + date[3] + ' 秒';
                if (date[1] === 0 && date[2] === 0 && date[3] === 0) {
                    location.reload();
                } else {
                    that.text(str);
                }
            }
        });
    });
}

function load_href(url, type = 1) {
    if (type == 1) {
        location.href = document.referrer;
    } else {
        location.href = url;
    }


}

function goBack() {
    window.history.go(-1)
}


